package com.ruoyi.webInfor.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @ClassName UpLoadUtils
 * @Description TODO 文件上传
 * @Author luguanqing
 * @Date 2021/4/7 8:53
 * @Version 1.0
 */
@RestController
@RequestMapping("/back/upload")
public class UploadController {

    /**
     * 文件上传
     */
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping("/uploadFile")
    public AjaxResult upload(MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            String fileName = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
            if(StringUtils.isNotEmpty(fileName)){
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileName);
                return ajax;
            }
        }
        return AjaxResult.error("上传异常");
    }

}
